package com.watson.websocket.myconfig;

/**
 * @author watson
 * @date 2021/8/4 17:01
 * @Description: Websocket发送自定义对象
 * 如果不在 @ServerEndpoint 指定endocers，直接通过sendObject(Object o)发送对象，
 * 会报javax.websocket.EncodeException: No encoder specified for object of class xxxx异常
 **/

import com.alibaba.fastjson.JSON;


import javax.websocket.Encoder;
import javax.websocket.EndpointConfig;
import java.util.Map;

public class WebSocketCustomEncoding implements Encoder.Text<Map> {
    @Override
    public String encode(Map user) {
        assert user != null;
        return JSON.toJSONString(user);
    }

    @Override
    public void init(EndpointConfig endpointConfig) {

    }

    @Override
    public void destroy() {

    }
}
